package com.dongke.controller;


import com.dongke.pojo.Car2;
import com.dongke.pojo.Tcar;
import com.dongke.service.CarService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("CarController")
public class CarController {

    @Autowired
    private CarService carService;

    //跳转展示页面
    @RequestMapping("list")
    public String getList(){

        return "list";
    }

    //查 询
    @RequestMapping("getViewthecar")
    @ResponseBody
    public PageInfo<Car2> getViewthecar(Car2 car2, @RequestParam(required = false,defaultValue = "1") Integer pageNum,@RequestParam(required = false,defaultValue = "3") Integer pageSize){
        PageInfo<Car2> pageInfo =  carService.selectgetCar(car2,pageNum,pageSize);
        return pageInfo;
    }

    @RequestMapping("toadd")
    public String toAdd(){
        return "add";
    }
    @RequestMapping("Caradditions")
    @ResponseBody
    public String Caradditions(Car2 car2){
        Integer i = carService.insertinto(car2);
        if(i > 0){
                return "1";
        }
        return "0";
    }
    //piliangshnchu
    @RequestMapping("deleteChecks")
    @ResponseBody
    public String deleteChecks(@RequestParam(value = "arr[]") Integer[] arr){
        carService.delete(arr);
        return "1";
    }
    //xiugaihuixain
    @RequestMapping("toupdate")
    public String toupdate(Integer cid, Model model){
         Car2 car2 = carService.selectCar(cid);
        model.addAttribute("list",car2);
        return "huixian";
    }

    //xiugai
    @RequestMapping("Modify")
    @ResponseBody
    public String Modify(Car2 car2){
        carService.update(car2);
        return "1";
    }
    //shanchu
    @RequestMapping("delete")
    public String delte(Integer cid){
        carService.deletes(cid);
        return "list";
    }
    //bootstraps客服端页面
    @RequestMapping("listClient")
    public String listClient(){
        return "bootstrap/listClient";
    }
    @RequestMapping("getViewthecars")
    @ResponseBody
    public List<Car2> getViewthecars(){
        return carService.selectCar2s();
    }

    //bootstraps服务端页面
    @RequestMapping("listServer")
    public String listServer(Model model){
        List<Tcar> list = carService.selectCar2();
        model.addAttribute("list",list);
        return "bootstrap/listServer";
    }

    @RequestMapping("getInfoServer")
    @ResponseBody
    public Map<String,Object> getInfoServer(
            Car2 car2
            ,@RequestParam(required = false,defaultValue = "1") Integer pageNum
            ,@RequestParam(required = false,defaultValue = "4") Integer pageSize

    ){

        PageInfo<Car2> list = carService.getInfoServer(car2,pageNum,pageSize);
        /*model.addAttribute("list",list);*/
        Map<String,Object> map = new HashMap<>();
        map.put("total",list.getTotal());
        map.put("rows",list.getList());
        return map;
    }

    //服务端批量删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public boolean deleteBatch(@RequestParam(value="ids[]") Integer[] ids) {//数组接收必须通过@RequestParam注解
        int i = carService.deleteBatch(ids);
        return i > 0;
    }
}
